package WebService;

import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.R.string;
import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

//Class to communicate with Web Service
public class ServiceCommunicate extends Activity{

	//Namespace of web service
	final static String NAMESPACE = "http://tempuri.org/";

	//Method to return boolean value from web service
	public Boolean checkDevice(String Imei,String SERVER_URL)
	{
		SoapObject request=new SoapObject(NAMESPACE, "CheckDevice");
		PropertyInfo pi=new PropertyInfo();
		pi.setName("IMEI");
		pi.setType(string.class);
		pi.setValue(Imei);
		request.addProperty(pi);
		SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=true;
		envelope.setOutputSoapObject(request);
        HttpTransportSE http = new HttpTransportSE("http://"+SERVER_URL+"/sale.asmx?WSDL");
        try{
        	http.call("http://tempuri.org/CheckDevice", envelope);
        	SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
        	int rs=response.toString().compareTo("true");
        	if(rs==0)
        	return true;
        	else 
        		return false;
        }catch(Exception e)
        {
        	return false;
        }
	}
	
	//Method to return boolean value from web service
	public Boolean ReturnBoolean(String Imei,String SERVER_URL)
	{
		SoapObject request=new SoapObject(NAMESPACE, "CheckDevice");
		PropertyInfo pi=new PropertyInfo();
		pi.setName("IMEI");
		pi.setType(string.class);
		pi.setValue(Imei);
		request.addProperty(pi);
		SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=true;
		envelope.setOutputSoapObject(request);
        HttpTransportSE http = new HttpTransportSE("http://"+SERVER_URL+"/sale.asmx?WSDL");
        try{
        	http.call("http://tempuri.org/CheckDevice", envelope);
        	SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
        	int rs=response.toString().compareTo("true");
        	if(rs==0)
        	return true;
        	else 
        		return false;
        }catch(Exception e)
        {
        	return false;
        }
	}
	
	//Method to return an SoapObject
	public SoapObject GetSoapObject(String name,String URL)
	{
				final  String SOAP_ACTION = "http://tempuri.org/GetProductByBarcode";
	    final String METHOD_NAME = "GetProductByBarcode";
	    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
	    PropertyInfo pi=new PropertyInfo();
	    pi.setName("Barcode");
	    pi.setValue(name);
	    pi.setType(String.class);
	    Request.addProperty(pi);
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE http = new HttpTransportSE("http://"+URL+"/sale.asmx?WSDL");
        try{
        	http.call(SOAP_ACTION, envelope);
        	SoapObject response=(SoapObject)envelope.getResponse();
        	response=(SoapObject)response.getProperty(1);
        	response=(SoapObject)response.getProperty(0);
        	response=(SoapObject)response.getProperty(0);

        	return response;
        }catch(Exception e){
        	return null;
        }
	}
        
	
      //Method to return an SoapObject
    	public String GetString(String name,String URL)
    	{
    		final  String SOAP_ACTION = "http://tempuri.org/getProductByBarcode";
    	    final String METHOD_NAME = "getProductByBarcode";
    	    URL="http://"+URL+"/sale.asmx?WSDL";
    	    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    	    PropertyInfo pi=new PropertyInfo();
    	    pi.setName("Barcode");
    	    pi.setValue(name);
    	    pi.setType(String.class);
    	    Request.addProperty(pi);
    	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(Request);
            HttpTransportSE http = new HttpTransportSE("http://"+URL+"/sale.asmx?WSDL");
            try{
            	http.call(SOAP_ACTION, envelope);
            	SoapObject response=(SoapObject)envelope.getResponse();
            	return response.toString();
            }catch(Exception e){
            	return null;
            }
     
	}
}
